<template>
  <div class="page-container">
    <!-- 页面头部 -->
    <div class="header">
      <div class="back-btn" @click="goBack">
        <Left />
      </div>
      <h1 class="title">我的优惠券</h1>
      <div class="coupons-count">{{ couponsStore.couponsCount }}张</div>
    </div>

    <!-- 状态统计 -->
    <div class="status-stats">
      <div class="stat-item" @click="setActiveTab('available')">
        <span class="stat-number">{{ couponsStore.availableCouponsCount }}</span>
        <span class="stat-label">可用</span>
      </div>
      <div class="stat-item" @click="setActiveTab('used')">
        <span class="stat-number">{{ couponsStore.usedCouponsCount }}</span>
        <span class="stat-label">已使用</span>
      </div>
      <div class="stat-item" @click="setActiveTab('expired')">
        <span class="stat-number">{{ couponsStore.expiredCouponsCount }}</span>
        <span class="stat-label">已过期</span>
      </div>
    </div>

    <!-- 搜索栏 -->
    <div class="search-bar">
      <input 
        v-model="searchKeyword" 
        type="text" 
        placeholder="搜索优惠券" 
        class="search-input"
        @input="handleSearch"
      />
    </div>

    <!-- 标签页 -->
    <div class="tabs">
      <div 
        class="tab-item" 
        :class="{ active: activeTab === 'all' }"
        @click="setActiveTab('all')"
      >
        全部
      </div>
      <div 
        class="tab-item" 
        :class="{ active: activeTab === 'available' }"
        @click="setActiveTab('available')"
      >
        可用
      </div>
      <div 
        class="tab-item" 
        :class="{ active: activeTab === 'used' }"
        @click="setActiveTab('used')"
      >
        已使用
      </div>
      <div 
        class="tab-item" 
        :class="{ active: activeTab === 'expired' }"
        @click="setActiveTab('expired')"
      >
        已过期
      </div>
    </div>

    <!-- 优惠券列表 -->
    <div class="coupons-content" v-if="displayedCoupons.length > 0">
      <div class="coupons-list">
        <div 
          class="coupon-item" 
          v-for="coupon in displayedCoupons" 
          :key="coupon.id"
          :class="coupon.status"
        >
          <div class="coupon-left">
            <div class="coupon-value">
              {{ couponsStore.getCouponDisplayValue(coupon) }}
            </div>
            <div class="coupon-type">{{ coupon.category }}</div>
          </div>
          
          <div class="coupon-right">
            <div class="coupon-info">
              <h4 class="coupon-name">{{ coupon.name }}</h4>
              <p class="coupon-desc">{{ coupon.description }}</p>
              <div class="coupon-conditions">
                <span v-if="coupon.minAmount > 0">满¥{{ coupon.minAmount }}可用</span>
                <span v-else>无门槛</span>
              </div>
            </div>
            
            <div class="coupon-status">
              <div class="status-badge" :class="coupon.status">
                {{ getStatusText(coupon.status) }}
              </div>
              <div class="coupon-date">
                {{ formatDate(coupon.endDate) }}到期
              </div>
            </div>
          </div>
          
          <div class="coupon-actions" v-if="coupon.status === 'available'">
            <nut-button 
              type="primary" 
              size="small"
              @click="useCoupon(coupon.id)"
              :disabled="!couponsStore.isCouponAvailable(coupon.id)"
            >
              立即使用
            </nut-button>
          </div>
        </div>
      </div>
    </div>

    <!-- 空状态 -->
    <div class="empty-state" v-else>
      <div class="empty-icon">🎫</div>
      <h3 class="empty-title">暂无优惠券</h3>
      <p class="empty-desc">去购物获得更多优惠券吧</p>
      <nut-button type="primary" @click="goToHome" class="empty-action">
        去逛逛
      </nut-button>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import { useRouter } from 'vue-router'
import { useCouponsStore } from '@/stores/coupons'
import { Left } from '@nutui/icons-vue'

const router = useRouter()
const couponsStore = useCouponsStore()

// 响应式数据
const searchKeyword = ref('')
const activeTab = ref('all')

// 计算属性
const displayedCoupons = computed(() => {
  let result = couponsStore.searchCoupons(searchKeyword.value)
  
  // 按标签页筛选
  if (activeTab.value !== 'all') {
    result = result.filter(coupon => coupon.status === activeTab.value)
  }
  
  return result
})

// 方法
const goBack = () => {
  router.go(-1)
}

const goToHome = () => {
  router.push('/')
}

const setActiveTab = (tab) => {
  activeTab.value = tab
}

const handleSearch = () => {
  // 搜索逻辑已在computed中处理
}

const useCoupon = (couponId) => {
  if (couponsStore.useCoupon(couponId)) {
    showToast('优惠券使用成功')
  } else {
    showToast('优惠券使用失败')
  }
}

const getStatusText = (status) => {
  const statusMap = {
    'available': '可用',
    'used': '已使用',
    'expired': '已过期'
  }
  return statusMap[status] || '未知'
}

const formatDate = (dateString) => {
  const date = new Date(dateString)
  const now = new Date()
  const diff = date - now
  
  if (diff < 0) {
    return '已过期'
  } else if (diff < 86400000) { // 1天内
    return '今天'
  } else if (diff < 86400000 * 7) { // 7天内
    return `${Math.ceil(diff / 86400000)}天后`
  } else {
    return date.toLocaleDateString()
  }
}

const showToast = (message) => {
  // 尝试使用NutUI的showToast
  if (window.nutui && window.nutui.showToast) {
    window.nutui.showToast(message)
  } else {
    // 创建简单的提示
    const toast = document.createElement('div')
    toast.className = 'simple-toast'
    toast.textContent = message
    document.body.appendChild(toast)
    
    setTimeout(() => {
      if (document.body.contains(toast)) {
        document.body.removeChild(toast)
      }
    }, 2000)
  }
}

// 生命周期
onMounted(() => {
  couponsStore.loadFromLocalStorage()
  couponsStore.initDefaultCoupons()
})

// 监听搜索关键词变化
watch(searchKeyword, () => {
  // 搜索逻辑已在computed中处理
})
</script>

<style scoped>
.page-container {
  min-height: 100vh;
  background: #f5f5f5;
  padding-bottom: 20px;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  background: white;
  border-bottom: 1px solid #f0f0f0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.back-btn {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #666;
}

.title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0;
  flex: 1;
  text-align: center;
}

.coupons-count {
  background: #ff4757;
  color: white;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
}

.status-stats {
  display: flex;
  background: white;
  margin: 12px 0;
  padding: 20px 16px;
}

.stat-item {
  flex: 1;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s;
}

.stat-item:hover {
  transform: translateY(-2px);
}

.stat-number {
  display: block;
  font-size: 24px;
  font-weight: 600;
  color: #ff4757;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 12px;
  color: #666;
}

.search-bar {
  background: white;
  padding: 16px;
  margin-bottom: 12px;
}

.search-input {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid #e0e0e0;
  border-radius: 24px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.3s;
}

.search-input:focus {
  border-color: #ff4757;
}

.tabs {
  display: flex;
  background: white;
  margin-bottom: 12px;
  border-bottom: 1px solid #f0f0f0;
}

.tab-item {
  flex: 1;
  text-align: center;
  padding: 16px;
  font-size: 14px;
  color: #666;
  cursor: pointer;
  transition: all 0.3s;
  border-bottom: 2px solid transparent;
}

.tab-item.active {
  color: #ff4757;
  border-bottom-color: #ff4757;
}

.coupons-content {
  padding: 0 16px;
}

.coupons-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.coupon-item {
  background: white;
  border-radius: 12px;
  padding: 16px;
  display: flex;
  gap: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: relative;
  overflow: hidden;
}

.coupon-item::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background: #ff4757;
}

.coupon-item.used::before {
  background: #999;
}

.coupon-item.expired::before {
  background: #ccc;
}

.coupon-left {
  flex-shrink: 0;
  text-align: center;
  padding: 8px;
  min-width: 80px;
}

.coupon-value {
  font-size: 20px;
  font-weight: 600;
  color: #ff4757;
  margin-bottom: 4px;
}

.coupon-type {
  font-size: 12px;
  color: #999;
}

.coupon-right {
  flex: 1;
  min-width: 0;
}

.coupon-name {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.coupon-desc {
  font-size: 14px;
  color: #666;
  margin: 0 0 8px 0;
  line-height: 1.4;
}

.coupon-conditions {
  font-size: 12px;
  color: #999;
  margin-bottom: 8px;
}

.coupon-status {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.status-badge {
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  color: white;
}

.status-badge.available {
  background: #52c41a;
}

.status-badge.used {
  background: #999;
}

.status-badge.expired {
  background: #ccc;
}

.coupon-date {
  font-size: 12px;
  color: #999;
}

.coupon-actions {
  flex-shrink: 0;
  display: flex;
  align-items: center;
}

.empty-state {
  text-align: center;
  padding: 80px 20px;
  background: white;
  border-radius: 8px;
  margin: 20px;
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 24px;
}

.empty-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0 0 12px 0;
}

.empty-desc {
  font-size: 14px;
  color: #999;
  margin: 0 0 24px 0;
  line-height: 1.5;
}

.empty-action {
  margin-top: 16px;
}

/* 简单提示样式 */
.simple-toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 14px;
  z-index: 9999;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .coupon-item {
    flex-direction: column;
    gap: 12px;
  }
  
  .coupon-left {
    align-self: center;
  }
  
  .coupon-actions {
    align-self: center;
  }
  
  .status-stats {
    padding: 16px 12px;
  }
  
  .stat-number {
    font-size: 20px;
  }
}
</style>
